from typing import Pattern
import xlrd
import xlwt


# 判断体温状态的函数
def get_status_by_temp(temp):
    if temp < 37.2:
        status_info = "正常"
    elif temp < 38.5:
        status_info = "发热"
    else:
        status_info = "高热"

    return status_info


# 根据不同的体温设置不同的颜色
def get_style_by_temp2(temp):
    style = xlwt.XFStyle()
    pattern = xlwt.Pattern()
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN
    if temp < 37.2:
        pattern.pattern_fore_colour = xlwt.Style.colour_map['green']
    elif temp < 38.5:
        pattern.pattern_fore_colour = xlwt.Style.colour_map['orange']
    else:
        pattern.pattern_fore_colour = xlwt.Style.colour_map['red']
    style.pattern = pattern

    return style


# 设置字体样式
def get_style_by_temp(temp):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    if temp < 37.2:
        font.colour_index = 3
    elif temp < 38.5:
        font.colour_index = 5
    else:
        font.colour_index = 2
    style.font = font
    return style


def main():
    wb = xlrd.open_workbook(r"C:\python06\day17\笔记\resources\体温测量登记表.xls")
    sheet = wb.sheet_by_index(0)
    data = []
    for row in range(1, sheet.nrows):
        record = []
        for col in range(sheet.ncols):
            record.append(sheet.cell(row, col).value)
        data.append(record)

    print(data)

    # 创建新的工作薄
    wb1 = xlwt.Workbook()
    sheet1 = wb1.add_sheet("带颜色的体温登记")
    sheet1.write(0, 0, "编号")
    sheet1.write(0, 1, "体温")
    sheet1.write(0, 2, "状态")

    # count 表示异常的人数,初始值为0
    count = 0
    for row_index, record in enumerate(data):
        no, temp = record  # 将数据解构赋值给no和temp,  no表示病人编号,temp表示体温
        # print(no,temp)
        if temp > 37.2:
            count += 1
        sheet1.write(row_index + 1, 0, no)
        sheet1.write(row_index + 1, 1, temp, get_style_by_temp2(temp))
        sheet1.write(row_index + 1, 2, get_status_by_temp(temp))

    sheet1.write(103, 0, '异常人数')
    sheet1.write(103, 1, f'{count}人')
    sheet1.write(104, 0, '总人数')
    sheet1.write(104, 1, f'{len(data)}人')

    wb1.save("带颜色的体温计.xls")


if __name__ == "__main__":
    main()
